
import {
	createUniFetch
} from 'uni-app-fetch'
import { storeToRefs } from 'pinia'
import pinia from '@/store/index.js'
import {
	useUserStore
} from '@/store/user'
import {
	constStore
} from '@/store/const'

const constState = constStore(pinia)
const {constInfo} = storeToRefs(constState)
// 配置符合自身业务的请求对象
console.log(constInfo.value.baseURL,'constInfoconstInfo');
const ongoingRequests = new Map();
export const uniFetch = createUniFetch({
	loading: { title: '正在加载...' },
	baseURL:constInfo.value.baseURL,
	intercept: {
		// 请求拦截器
		request(options) {
			const userState = useUserStore()
			// 全局公共头信息
		const arr ="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoiMTAwMDA3Iiwic291cmNlVHlwZSI6IjIiLCJpc3MiOiJxYl90ZWNoIiwibW9iaWxlIjoiMTUxODE2Nzk4MDciLCJleHAiOjE3MzUxMTUyNDQ0NjMsImlhdCI6MTczNTEwNTE2NDQ2Mywia2V5IjoiOWUzZmMyYTZkMGY0NWM3YTk5OWFiMDFlYmNhY2FmOTQiLCJ1c2VybmFtZSI6IjEwMDAwNyJ9.53jWV4U0FzTOTvOBQWfND8WioJ5iXZC31Qpvqz1Vee_Aiaxc4agb6s49cWKmm9DWJj2IVqrEKcP47gxHeGFSHw"
			const defaultHeaders = {
				Authorization: `Bearer ${userState.token}`
				// Authorization: `Bearer ${arr}`
			}
			// 自定义头信息（token）
			options.header = Object.assign({}, defaultHeaders, options.header, )
			// 后续补充实际逻辑
			return options
		},
		// 响应拦截器
		response({
			statusCode,
			data,
			config
		}) {
			// 后续补充实际逻辑
		console.log(data,'data');
			if(data.code == 404 || data.code == 'black'){
					uni.reLaunch({
						url: '/pages/clb/404'
					});
			} else if(data.code == '401'){
				uni.navigateTo({
					url:'/pages/h5-login/h5-login'
				})
			}else {
				return data
			}
		},
		error(error) {
			console.log(error, 'error');
			throw error;
		},
	},
})